package com.atguigu.gmall.common.config;

import com.atguigu.gmall.common.properties.ThreadPoolProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableConfigurationProperties(value = ThreadPoolProperties.class)
public class ThreadPoolExecutorConfiguration {

    @Autowired
    private ThreadPoolProperties threadPoolProperties ;

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(
                        threadPoolProperties.getCorePoolSize() ,
                        threadPoolProperties.getMaximumPoolSize() ,
                        threadPoolProperties.getKeepAliveTime() ,
                        TimeUnit.SECONDS , new ArrayBlockingQueue<>(threadPoolProperties.getWorkQueueSize()) ,
                Executors.defaultThreadFactory() , new ThreadPoolExecutor.AbortPolicy()
        ) ;
        return threadPoolExecutor ;
    }

}
